package com.ruimei.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruimei.entitys.*;
import com.ruimei.entitys.enums.Relation;
import com.ruimei.mapper.StudentMapper;
import com.ruimei.mapper.UserMapper;
import com.ruimei.service.IParentService;
import com.ruimei.util.CommUtil;
import com.ruimei.util.Md5Encrypt;
import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class IParentServiceImpl
  implements IParentService
{

  @Resource
  private RedisTemplate<String, String> redisTemplate;

  @Resource
  private UserMapper mapper;

  @Resource
  private StudentMapper studentMapper;

  @Override
  public String selectUser(Parent parent)
  {
    return this.mapper.selectUserExist(parent);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public JSONObject parentRegister(ParentRegisterVo parentRegisterVo) {
    JSONObject object = new JSONObject();
    JSONObject data = new JSONObject();
    //首先验证是否有对应的学生
    String studentId = mapper.selectStudentExists(parentRegisterVo.getStudentIdCard());
    if(StringUtils.isBlank(studentId)){
        data.put("status",false);
        data.put("msg","注册失败，请检查学生的身份证是否输入正确。如有疑问请与班主任联系！");
        object.put("status",true);
        object.put("data",data);


      return object;
    }
    String id = mapper.selectParentExists(parentRegisterVo.getPhoneNo());
    if(StringUtils.isBlank(id)){
      //如果成功匹配结果则先注册家长，再绑定映射。
      String salt = CommUtil.randomString(6);
      String pwd  = Md5Encrypt.md5(Md5Encrypt.md5(parentRegisterVo.getPwd()) + salt);
      String uid = UUID.randomUUID().toString().replace("-","");
      int r = mapper.insertNewParent(uid,parentRegisterVo.getPhoneNo(),pwd,salt,parentRegisterVo.getName());
      if(r>0){
        //查询绑定关系是否存在
        String phoneNo = mapper.selectMappingExists(studentId,parentRegisterVo.getRelation().toString());
        if(StringUtils.isBlank(phoneNo)){
          int row = mapper.insertParentStudentMapping(studentId,uid, parentRegisterVo.getRelation().toString());
          if(row > 0){
              data.put("status",true);
              data.put("msg","注册成功!");
              object.put("status",true);
              object.put("data",data);

            return object;
          }
            data.put("status",false);
            data.put("msg","关系绑定错误！");
            object.put("status",true);
            object.put("data",data);

          return object;
        }
        mapper.deleteParentById(uid);
          data.put("status",false);
          data.put("msg","注册失败！该学生已经被手机号为"+phoneNo+"的家长绑定！");
          object.put("status",true);
          object.put("data",data);

        return object;

      }
        data.put("status",false);
        data.put("msg","服务器繁忙，请稍后再试！");
        object.put("status",true);
        object.put("data",data);

      return object;
    }
      data.put("status",false);
      data.put("msg","该家长已存在！");
      object.put("status",true);
      object.put("data",data);

    return object;

  }

  @Override
  public JSONObject updateParentInfo(Parent parent) {
    JSONObject object = new JSONObject();
    JSONObject data = new JSONObject();
    if(mapper.updateParentInfo(parent) >0){
        data.put("status",true);
        data.put("msg","资料保存成功");
        object.put("status",true);
        object.put("data",data);
        redisTemplate.delete(parent.getId());
        redisTemplate.opsForValue().set(parent.getId(),JSONObject.toJSONString( mapper.getParentById(parent.getId())));

      return object;
    }
      data.put("status",false);
      data.put("msg","资料保存失败");
      object.put("status",true);
      object.put("data",data);

    return object;
  }

  @Override
  public JSONObject updateStudentInfo(Student student) {
    JSONObject object = new JSONObject();
      JSONObject data = new JSONObject();
    if(studentMapper.updateStudent(student) > 0){
        data.put("status",true);
        data.put("msg","修改成功");
        object.put("status",true);
        object.put("data",data);

      return object;
    }
      data.put("status",false);
      data.put("msg","修改失败");
      object.put("status",true);
      object.put("data",data);

    return object;
  }

  @Override
  public JSONObject getStudentHobby(String id) {
    JSONObject object = new JSONObject();
      JSONObject data = new JSONObject();
    List<Hobby> hobbies = studentMapper.selectStudentHobby(id) ;
    if(hobbies.size() > 0){
        data.put("status",true);
        data.put("msg","查询成功");
        data.put("data",hobbies);
        object.put("status",true);
        object.put("data",data);

      return object;
      }
      data.put("status",false);
      data.put("msg","该学生暂时还没有添加爱好特长记录");
      object.put("status",true);
      object.put("data",data);

    return object;
  }

  @Override
  public JSONObject saveStudentHobby(Hobby hobby) {
    JSONObject object = new JSONObject();
    JSONObject data = new JSONObject();
    //首先查询是否存在，存在更新，反之新增
   if(StringUtils.isBlank(hobby.getId())){
     if(studentMapper.insertHobby(hobby) > 0){
         data.put("status",true);
         data.put("msg","学生爱好添加成功");
       object.put("status",true);
       object.put("data",data);
       return object;
     }
       data.put("status",false);
       data.put("msg","学生爱好添加失败");
     object.put("status",true);
     object.put("data",data);
     return object;
   }
   //以下情况是存在目标记录，直接更新即可
    if(studentMapper.updateHobby(hobby) > 0){
        data.put("status",true);
        data.put("msg","学生爱好保存成功");
      object.put("status",true);
      object.put("data",data);
      return object;
    }
      data.put("status",false);
      data.put("msg","学生爱好保存失败");
    object.put("status",true);
    object.put("data",data);
    return object;
  }

  @Override
  public JSONObject queryStudentReward(String id) {
    JSONObject object = new JSONObject();
    JSONObject data = new JSONObject();
    List<Reward> rewards = studentMapper.selectStudentReward(id) ;
    if(rewards.size() > 0){
      data.put("status",true);
      data.put("msg","查询成功");
      data.put("data",rewards);
      object.put("status",true);
      object.put("data",data);
      return object;
    }
    data.put("status",false);
    data.put("msg","该学生暂时还没有添加奖惩记录");
      object.put("status",true);
      object.put("data",data);
    return object;
  }

  @Override
  public JSONObject saveStudentReward(Reward reward) {
    JSONObject object = new JSONObject();
      JSONObject data = new JSONObject();
    //首先查询是否存在，存在更新，反之新增
    if(StringUtils.isBlank(reward.getId())){
      if(studentMapper.insertReward(reward) > 0){
        data.put("status",true);
        data.put("msg","学生奖惩记录添加成功");
          object.put("status",true);
          object.put("data",data);
        return object;
      }
      data.put("status",false);
      data.put("msg","学生奖惩记录添加失败");
        object.put("status",true);
        object.put("data",data);
      return object;
    }
    //以下情况是存在目标记录，直接更新即可
    if(studentMapper.updateReward(reward) > 0){
      data.put("status",true);
      data.put("msg","学生奖惩记录保存成功");
        object.put("status",true);
        object.put("data",data);
      return object;
    }
    data.put("status",false);
    data.put("msg","学生奖惩记录保存失败");
      object.put("status",true);
      object.put("data",data);
    return object;
  }

  @Override
  public JSONObject bindStudent(String parentId,String studentIdCard,String relation) {
    JSONObject object = new JSONObject();
    JSONObject data = new JSONObject();
    //查询学生id
       String id = studentMapper.selectStudentId(studentIdCard);
       if(StringUtils.isBlank(id)){
           data.put("status",false);
           data.put("msg","不存在该学生，请确认身份证是否正确");
           object.put("status",true);
           object.put("data",data);
           return object;
       }
    String r = studentMapper.selectBindexists(id,relation);
    if(StringUtils.isBlank(r)){
      int row = studentMapper.bindRelation(parentId,id,relation);
      if(row > 0){
        data.put("status",true);
        data.put("msg","关系绑定成功");
          object.put("status",true);
          object.put("data",data);
        return object;
      }
      data.put("status",false);
      data.put("msg","绑定失败，请稍后重试");
        object.put("status",true);
        object.put("data",data);
      return object;
    }
    data.put("status",false);
    data.put("msg","该学生的"+Relation.matching(r)+"关系已经被绑定");
      object.put("status",true);
      object.put("data",data);
    return object;
  }

    @Override
    public JSONObject selectCurrentParentChild(String id) {
      JSONObject object = new JSONObject();
      List<Student> list = mapper.getStudents(id);
        object.put("status",true);
        object.put("msg","查询成功");
        object.put("data",list);
        return object;
    }

    @Override
  public Parent login(Parent parent)
  {
    parent.setSecretkey(Md5Encrypt.MdPwd(parent.getSecretkey(), parent.getSalt()));
    Parent parentt = this.mapper.selectPass(parent);
    if(null == parentt) {
    	return null;
    }
    String fid = String.valueOf(parentt.getFirsted());
    String id = parentt.getId();
    if (fid.equals("1"))
    {
      if(redisTemplate.hasKey(id))
      {

      }
      else {
        redisTemplate.opsForValue().set(id, JSONObject.toJSONString(parentt));
      }
    }
    else if (fid.equals("0"))
    {
      if(redisTemplate.hasKey(id))
      {

      }
      else {
        redisTemplate.opsForValue().set(id, JSONObject.toJSONString(parentt));
      }
    }
    if (StringUtils.isNoneBlank(new CharSequence[] { id }))
    {
      redisTemplate.opsForValue().set("student_"+id,JSONObject.toJSONString(this.mapper.getStudents(id)));
      Parent p = new Parent();
      p.setLastLoginTime(new Date());
      p.setId(id);
      if(parentt.getFirsted() ==1){
        p.setFirsted(0);
      }
      mapper.updateParentInfo(p);
      return parentt;
    }
    return null;
  }
}


/* Location:              E:\360MoveData\Users\admin\Desktop\teacher_wap-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes\com\ruimei\service\impl\IParentServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */